package itheima.edu.info.manager.service;

import itheima.edu.info.manager.dao.TeacherDao;
import itheima.edu.info.manager.domain.Teacher;

public class TeacherService {
    //业务员
    //1.创建一个TeacherDao库管对象
    private TeacherDao teacherDao = new TeacherDao();

    public boolean addTeacher(Teacher tea) {
        //2.将获取到的Student对象传入到StudentDao中
        //3.将StudentDao中返回的结果返回给方法调用者
        return teacherDao.addTeacher(tea);
    }

    public boolean isExists(String id) {
        TeacherDao teacherDao = new TeacherDao();
        Teacher[] teas=teacherDao.findAllTeacher();
        //假设id在数组中不存在
        boolean exists=false;
        for (int i = 0; i < teas.length; i++) {
            Teacher teacher=teas[i];
            if (teacher!=null&&teacher.getId().equals(id)){
                exists=true;
                break;
            }
        }
        return exists;
    }

    public Teacher[] findAllTeacher() {
        //1.调用库管对象的findAllTeacher获取老师对象数组
        Teacher[] allTeacher=teacherDao.findAllTeacher();
        //2.判断数组中是否有学生信息（有：返回地址， 没有返回null）
        //数组中只要存在一个不是null的元素 ，那就代表有学生信息
        boolean flag=false;
        for (int i = 0; i < allTeacher.length; i++) {
            Teacher tea=allTeacher[i];
            if (tea!=null){
                flag=true;
                break;
            }
        }
        if (flag){
            return allTeacher;
        }else {
            return null;
        }
    }

    public void deleteTeacherById(String delId) {
        teacherDao.deleteTeacherById(delId);
    }

    public void updateTeacher(String updateId, Teacher newTea) {
        teacherDao.updateTeacher(updateId,newTea);
    }
}
